package com.example.gcmtest1;

import java.io.File;
import java.util.ArrayList;

import com.example.gcmtest1.httpserver.HTTPProxyService;
import com.example.gcmtest1.youtube.MyRandomWriter;
import com.tnsw.coreutils.AsyncManager;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class ListViewActivity extends Activity implements OnItemClickListener {

	public static AsyncManager asyncManager;
	private String logName = "ListViewActivity";
	
	public ListViewActivity() {
		Log.d(logName, "Constructor is created");
		asyncManager = new AsyncManager();
		
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(logName, "onCreated()");
		
        asyncManager.appendNetworkLog("Hello");
        Log.d(logName, "tested networklog");
        
        setContentView(R.layout.activity_list_view);
        // Test
        
        View lv1 = (View) this.getWindow().getDecorView();
        if (lv1 == null) {
        	Log.d(logName, "lv1 is null!!");
        } else {
        	Log.d(logName, "lv1 is not null!!");
        }
        
        ListView lv = (ListView) lv1.findViewById(R.id.lstList1);
        lv.setOnItemClickListener(this);
        
        Intent intent = new Intent(this, HTTPProxyService.class);
        this.startService(intent);
        
        Log.d(logName, "Called startService() from Activity");
        
        String dbFile = this.getDatabasePath("mydb1.db").getAbsolutePath();
        Log.d(logName, "searching for existing mydb1.db first, at "  + dbFile);
        
        File f = new File(this.getDatabasePath("mydb1.db").getAbsolutePath());
        if (f.exists() == false) {
        	Log.e(logName, "Cannot find " + dbFile);
        	try {
        		MyRandomWriter.copyFile(this.getAssets().open("mydb1.db"), dbFile);
        		Log.d(logName, "Copied file done");
        	} catch (Exception e) {
        		Log.e(logName, "Cannot copy file");
        		e.printStackTrace();
        	}
        	
        } else {
        	Log.d(logName, "Found existing " + dbFile);
        	boolean b = f.delete();
        	if (b == true) {
        		Log.d(logName, "deleted");
        		try {
        			MyRandomWriter.copyFile(this.getAssets().open("mydb1.db"), dbFile);
            		Log.d(logName, "Copied file done");	
        		} catch (Exception e) {
        			Log.e(logName, "Cannot copy file");
            		e.printStackTrace();	
        		}
        		
        	}
        }
        
        
        MySqlHelper q = new MySqlHelper(this);
        MySqlHelper q2 = new MySqlHelper(this);
        try {
        	SQLiteDatabase db1 = q.getWritableDatabase();
        	SQLiteDatabase db2 = q2.getWritableDatabase();
        	Log.d(logName, "Got database");
        	db1.execSQL("insert into table1(id, name) values(2, 'Pimsai');");
        	Log.d(logName, "Inserted table");
        	db1.close();
        	db2.close();
        } catch (Exception e) {
        	Log.e(logName, e.toString());
        }
        
        Log.d(logName, "data_dir=" + this.getApplicationInfo().dataDir);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_list_view, menu);
        return true;
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		TextView tv = (TextView) arg1;
		String selectedText = (String) tv.getText();
		
		Log.d(logName, selectedText);
		Resources r = this.getResources();
		String[] s = r.getStringArray(R.array.array1);
		if (selectedText.equals(s[0])) {
			// Use Account Manager
			Log.d(logName, "going to use AccountManager");
			Intent intent = new Intent(this, com.example.gcmtest1.AccountManagerActivity.class);
			
			this.startActivity(intent);
		} else if (selectedText.equals(s[1])) {
			// Use WebView for Google
			Log.d(logName, "going to use AccountManager");
			Intent intent = new Intent(this, com.example.gcmtest1.GoogleWebView.class);
			
			this.startActivity(intent);
			
		} else if (selectedText.equals(s[2])) {
			Intent intent = new Intent(this, com.example.gcmtest1.TranslucentActivity.class);
			
			this.startActivity(intent);
		
		} else if (selectedText.equals(s[3])) {
			
			
		} else if (selectedText.equals(s[4])) {
			Intent intent = new Intent(this, com.example.gcmtest1.MediaPlayer1.class);
			
			this.startActivity(intent);
			
		} else if (selectedText.equals(s[5])) {
			Intent intent = new Intent(this, com.example.gcmtest1.VideoPlayer1.class);
			
			this.startActivity(intent);
		} else if (selectedText.equals(s[6])) {
			Intent intent = new Intent(this, com.example.gcmtest1.WebServer1.class);
			
			this.startActivity(intent);
			
			
		} else if (selectedText.equals(s[7])) {
			Log.d(logName, "Before create Intent");
			Intent intent = new Intent(this, com.example.gcmtest1.FileListActivity.class);
			Log.d(logName, "Before Start Activity FileListActivity");
			
			this.startActivity(intent);
			Log.d(logName, "this.startActivity() is called");
			
		} else if (selectedText.equals(s[8])) {
			Intent intent = new Intent(this, com.example.gcmtest1.YoutubeActivity.class);
			
			this.startActivity(intent);
		} else if (selectedText.equals(s[9])) {
			
			Intent intent = new Intent(this, com.example.gcmtest1.Table1Activity.class);
			
			this.startActivity(intent);
		} 
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(logName, "onResume()");
	}
	
    @Override
    protected void onPause() {
    	super.onPause();
    	Log.d(logName, "onPause()");
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	Log.d(logName, "onStop()");
    }
    
    @Override 
    protected void onDestroy() {
    	super.onDestroy();
    	Log.d(logName, "onDestroy()");
    }
    
    @Override
    public void onBackPressed() {
    	//Log.d(logName, "onBackPressed()");
    	super.onBackPressed();
    }
}
